﻿using AdminStyem.Application.Command.FeedstockCommand;
using AdminStyem.Domain.Entity;
using AdminStyem.Repositories;
using AutoMapper;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminStyem.Application.CommandHandler.FeedstockHandler
{
    public class FeedstockAddCommandHandler : IRequestHandler<FeedstockAddCommand, APIResult<object>>
    {
        IBaseRespository<Feedstock> BaseFeedstock;
        IMapper Mapper;
        public FeedstockAddCommandHandler(IBaseRespository<Feedstock> baseFeedstock, IMapper mapper)
        {
            BaseFeedstock = baseFeedstock;
            Mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(FeedstockAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> ret = new APIResult<object>();
            try
            {
                var feedstock = Mapper.Map<Feedstock>(request);
                await BaseFeedstock.add(feedstock);
                ret.Code = ResultCode.添加成功;
                ret.Message = ResultCode.添加成功.ToString();
                ret.Data = feedstock;
            }
            catch (Exception ex)
            {
                ret.Code = ResultCode.添加失败;
                ret.Message = ex.Message;
            }
            return ret;
        }
    }
}
